/**
 * 
 */
package com.revenco.bi.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.revenco.bi.controller.basecontroller.AbstractUserAwareController;
import com.revenco.bi.model.TnChartBaseinfo;
import com.revenco.bi.service.TnChartBaseinfoService;
import com.revenco.bi.service.ViewEditChartService;

/**
 * 预览编辑图表
 * 
 * @author zhaozz
 * 
 */
@Controller
public class ViewEditChart extends AbstractUserAwareController {
	@Autowired
	ViewEditChartService vecService;
	@Autowired
	private TnChartBaseinfoService tnChartBaseinfoService;

	@RequestMapping(value = "/viewEditChart", produces = "text/plain;charset=UTF-8")
	@ResponseStatus(HttpStatus.OK)
	public String getChartinfo(Model model, HttpServletResponse response,
			@RequestParam("chartId") String id, HttpServletRequest request)
			throws Exception {
		String params = request.getParameter("params");
		if (!StringUtils.isEmpty(params)) {
			params = new String(params.getBytes("ISO8859-1"), "UTF-8");
		}
		String chartXml = vecService.getChartinfo(model, response, id, params);
		model.addAttribute("params", params);
		return chartXml;
	}

	@RequestMapping(value = "/chart/chartDetail", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String getChartDetail(String field,
			@RequestParam("chartId") String id) throws Exception {
		String jsonStr = vecService.getChartDetail(field, id);
		return jsonStr;
	}

	@RequestMapping(value = "/pieceSql", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String pieceSql(String field, @RequestParam("chartId") String id, String mulRelationData)
			throws Exception {
		String jsonStr = vecService.pieceSql(id, mulRelationData);
		return jsonStr;
	}

	@RequestMapping(value = "/getChartinfoById")
	@ResponseBody
	public TnChartBaseinfo getChartList(@RequestParam("chartId") String id) {
		return tnChartBaseinfoService.getChartinfoById(id);
	}

	/**
	 * url链接直接访问跳转指定jsp页面
	 */
	@RequestMapping(value = "/editChart", produces = "text/plain;charset=UTF-8")
	@ResponseStatus(HttpStatus.OK)
	public String show(Model model, HttpServletRequest request,
			@RequestParam("id") String id) {
		return "/chart/editChart";
	}
}
